﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Summerset.Isbndb {


    /// <summary>
    /// Returns the usage statistics for the current Isbndb object's ApiKey
    /// </summary>
    /// <remarks>
    /// This keystat data is returned which each Response Page, so it's only as recent as the last Page Request.
    /// </remarks>
    public class Keystats {

        // Holds a reference to a single XmlNode containing Keystats data.
        readonly XmlNode _keystatsNode;

        /// <summary>
        /// Initializes a new instance of the <see cref="Keystats"/> class.
        /// </summary>
        public Keystats(XmlNode KeystatsNode) {

            // Make sure the XmlNode provided is actually the KeyStats node.
            if (KeystatsNode.Name != "KeyStats")
                throw new ArgumentException("The provided XmlNode does not appear to be valid for Keystats object.");

            // Set the local keystats field.
            _keystatsNode = KeystatsNode;

        }


        /// <summary>
        /// Gets the API key that is used when issuing requests.
        /// </summary>
        public string ApiKey {
            get {
                return _keystatsNode.Attributes["access_key"].Value;
            }
        }

        /// <summary>
        /// Gets the number of requests granted by the API during the current usage period.
        /// </summary>
        /// <remarks>
        /// As of July 2012, the usage period is 24 hours.
        /// </remarks>
        public int GrantedRequests {
            get {
                return int.Parse(_keystatsNode.Attributes["granted"].Value);
            }
        }

        /// <summary>
        /// Gets the total requests, including rejected requests issued against the API
        /// during the current usage period.
        /// </summary>
        /// <remarks>
        /// As of July 2012, the usage period is 24 hours.
        /// </remarks>
        public int TotalRequests {
            get {
                return int.Parse(_keystatsNode.Attributes["requests"].Value);
            }
        }

        /// <summary>
        /// Gets the maximum number of requests that may be issued against the API during 
        /// a given usage period.
        /// </summary>
        /// <remarks>
        /// As of July 2012, the usage period is 24 hours.
        /// </remarks>
        public int RequestsLimit {
            get {
                return int.Parse(_keystatsNode.Attributes["limit"].Value);
            }
        }



    }
}
